$(function () {
    $("#tb").datagrid({
        title: "我的订单",
        url: 'Orders/getAll',
        fitColumns: true,
        singleSelect: true,
        pagination: true,//分页属性
        columns: [[
            {field: 'onumber', title: '订单编号', width: 100},
            {field: 'trainno', title: '车次号', width: 100},
            {
                field: 'sell_time', title: '购票时间', width: 100,
                formatter: function (value) {
                    if (!isNaN(value)) {
                        var Timestamp = new Date(value);
                        return Timestamp.toLocaleString();
                    }
                }
            },
            {field: 'price', title: '票价', width: 100},
            {
                field: 'state', title: '订单状态', width: 100,
                formatter: function (value) {
                    if (!isNaN(value)) {
                        if (value == 1) {
                            return "未付款";
                        }
                        if (value == 2) {
                            return "已付款";
                        }
                        if (value == 3) {
                            return "已退款";
                        }
                        if (value == 4) {
                            return "已取票";
                        }
                    } else {
                        return value;
                    }
                }
            }
        ]],
        view: detailview,
        detailFormatter: function (index, row) {
            if (!isNaN(row.departure_time)) {
                var Timestamp = new Date(row.departure_time);
                row.departure_time = Timestamp.toLocaleString();
            }
            if (!isNaN(row.arrival_time)) {
                var Timestamp = new Date(row.arrival_time);
                row.arrival_time = Timestamp.toLocaleString();
            }
            if (!isNaN(row.state)) {
                var value = row.state;
                var btn = "";
                if (value == 1) {
                    btn = "<button onclick='cancelo(" + row.oid + ")'  class=\"easyui-linkbutton\" style=\"width: 80px\">取消</button>" + "<button onclick='pay(" + row.oid + ")'  class=\"easyui-linkbutton\" style=\"width: 80px\">付款</button>";
                }
                if (value == 2) {
                    btn = "<button onclick='cancelo(" + row.oid + ")'  class=\"easyui-linkbutton\" style=\"width: 80px\">取消</button>" + "<button onclick='print(" + row.oid + ")'  class=\"easyui-linkbutton\" style=\"width: 80px\">取票</button>";
                }
                if (value == 3) {
                    btn = "已退款";
                }
                if (value == 4) {
                    btn = "已取票";
                }
            }
            return '<div><table><tr>' +
                '<p>订单id: ' + row.oid + '</p>' +
                '<p>车次号: ' + row.trainno + '</p>' +
                '<p>出发时间: ' + row.departure_time
                + '</p>' +
                '<p>到达时间: ' + row.arrival_time + '</p>' +
                '<p>始发站: ' + row.dstation + '</p>' +
                '<p>终点站: ' + row.terminal + '</p>' +
                '<p>票价: ' + row.price + '</p>' +
                '<p>车厢号: ' + row.carriage + '</p>' +
                '<p>座位号: ' + row.seatno + '</p>' +
                '<p> ' + btn + '</p>' +
                '</tr></table></div>';
        }
    });
});

function createXMLHTTPRequest() {

    // 1.创建XMLHttpRequest对象
    // 这是XMLHttpReuquest对象无部使用中最复杂的一步
    // 需要针对IE和其他类型的浏览器建立这个对象的不同方式写不同的代码
    var xmlHttpRequest;
    if (window.XMLHttpRequest) {
        // 针对FireFox，Mozillar，Opera，Safari，IE7，IE8
        xmlHttpRequest = new XMLHttpRequest();
        // 针对某些特定版本的mozillar浏览器的BUG进行修正
        if (xmlHttpRequest.overrideMimeType) {
            xmlHttpRequest.overrideMimeType("text/xml");
        }
    } else if (window.ActiveXObject) {
        // 针对IE6，IE5.5，IE5
        // 两个可以用于创建XMLHTTPRequest对象的控件名称，保存在一个js的数组中
        // 排在前面的版本较新
        var activexName = ["MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
        for (var i = 0; i < activexName.length; i++) {
            try {
                // 取出一个控件名进行创建，如果创建成功就终止循环
                // 如果创建失败，回抛出异常，然后可以继续循环，继续尝试创建
                xmlHttpRequest = new ActiveXObject(activexName[i]);
                if (xmlHttpRequest) {
                    break;
                }
            } catch (e) {
            }
        }
    }

    return xmlHttpRequest;
}

function cancelo(oid) {
    var req = createXMLHTTPRequest();
    if (req) {
        req.open("POST", "Orders/cancelOrder", true);
        req.setRequestHeader("Content-Type",
            "application/x-www-form-urlencoded; charset=utf-8;");
        req.send("oid=" + oid);
        req.onreadystatechange = function () {
            if (req.readyState == 4) {
                if (req.status == 200) {
                    var msg = req.responseText;
                    if (msg == "fail1") {
                        alert("订单不存在");
                    }
                    else if (msg == "error1") {
                        alert("订单状态异常，请刷新界面");
                    }
                    else if (msg == "success") {
                        alert("退票成功，请刷新页面查看最新信息");
                    }
                    $("#tb").datagrid({
                        title: "我的订单",
                        url: 'Orders/getAll',
                        fitColumns: true,
                        singleSelect: true,
                        pagination: true,//分页属性
                        columns: [[
                            {field: 'onumber', title: '订单编号', width: 100},
                            {field: 'trainno', title: '车次号', width: 100},
                            {
                                field: 'sell_time', title: '购票时间', width: 100,
                                formatter: function (value) {
                                    if (!isNaN(value)) {
                                        var Timestamp = new Date(value);
                                        return Timestamp.toLocaleString();
                                    }
                                }
                            },
                            {field: 'price', title: '票价', width: 100},
                            {
                                field: 'state', title: '订单状态', width: 100,
                                formatter: function (value) {
                                    if (!isNaN(value)) {
                                        if (value == 1) {
                                            return "未付款";
                                        }
                                        if (value == 2) {
                                            return "已付款";
                                        }
                                        if (value == 3) {
                                            return "已退款";
                                        }
                                        if (value == 4) {
                                            return "已取票";
                                        }
                                    } else {
                                        return value;
                                    }
                                }
                            }
                        ]],
                        view: detailview,
                        detailFormatter: function (index, row) {
                            if (!isNaN(row.departure_time)) {
                                var Timestamp = new Date(row.departure_time);
                                row.departure_time = Timestamp.toLocaleString();
                            }
                            if (!isNaN(row.arrival_time)) {
                                var Timestamp = new Date(row.arrival_time);
                                row.arrival_time = Timestamp.toLocaleString();
                            }
                            if (!isNaN(row.state)) {
                                var value = row.state;
                                var btn = "";
                                if (value == 1) {
                                    btn = "<button onclick='cancelo(" + row.oid + ")'  class=\"easyui-linkbutton\" style=\"width: 80px\">取消</button>" + "<button onclick='pay(" + row.oid + ")'  class=\"easyui-linkbutton\" style=\"width: 80px\">付款</button>";
                                }
                                if (value == 2) {
                                    btn = "<button onclick='cancelo(" + row.oid + ")'  class=\"easyui-linkbutton\" style=\"width: 80px\">取消</button>" + "<button onclick='print(" + row.oid + ")'  class=\"easyui-linkbutton\" style=\"width: 80px\">取票</button>";
                                }
                                if (value == 3) {
                                    btn = "已退款";
                                }
                                if (value == 4) {
                                    btn = "已取票";
                                }
                            }
                            return '<div><table><tr>' +
                                '<p>订单id: ' + row.oid + '</p>' +
                                '<p>车次号: ' + row.trainno + '</p>' +
                                '<p>出发时间: ' + row.departure_time
                                + '</p>' +
                                '<p>到达时间: ' + row.arrival_time + '</p>' +
                                '<p>始发站: ' + row.dstation + '</p>' +
                                '<p>终点站: ' + row.terminal + '</p>' +
                                '<p>票价: ' + row.price + '</p>' +
                                '<p>车厢号: ' + row.carriage + '</p>' +
                                '<p>座位号: ' + row.seatno + '</p>' +
                                '<p> ' + btn + '</p>' +
                                '</tr></table></div>';
                        }
                    });

                } else {
                    alert("error");
                }
            }
        }
    }

}

function pay(oid) {
    var req = createXMLHTTPRequest();
    if (req) {
        req.open("POST", "Orders/payOrder", true);
        req.setRequestHeader("Content-Type",
            "application/x-www-form-urlencoded; charset=utf-8;");
        req.send("oid=" + oid);
        req.onreadystatechange = function () {
            if (req.readyState == 4) {
                if (req.status == 200) {
                    var msg = req.responseText;
                    if (msg == "fail1") {
                        alert("订单不存在");
                    }
                    else if (msg == "error1") {
                        alert("订单状态异常，请刷新界面");
                    }
                    else if (msg == "success") {
                        alert("付款成功，请刷新页面查看最新信息");
                    }
                    $("#tb").datagrid({
                        title: "我的订单",
                        url: 'Orders/getAll',
                        fitColumns: true,
                        singleSelect: true,
                        pagination: true,//分页属性
                        columns: [[
                            {field: 'onumber', title: '订单编号', width: 100},
                            {field: 'trainno', title: '车次号', width: 100},
                            {
                                field: 'sell_time', title: '购票时间', width: 100,
                                formatter: function (value) {
                                    if (!isNaN(value)) {
                                        var Timestamp = new Date(value);
                                        return Timestamp.toLocaleString();
                                    }
                                }
                            },
                            {field: 'price', title: '票价', width: 100},
                            {
                                field: 'state', title: '订单状态', width: 100,
                                formatter: function (value) {
                                    if (!isNaN(value)) {
                                        if (value == 1) {
                                            return "未付款";
                                        }
                                        if (value == 2) {
                                            return "已付款";
                                        }
                                        if (value == 3) {
                                            return "已退款";
                                        }
                                        if (value == 4) {
                                            return "已取票";
                                        }
                                    } else {
                                        return value;
                                    }
                                }
                            }
                        ]],
                        view: detailview,
                        detailFormatter: function (index, row) {
                            if (!isNaN(row.departure_time)) {
                                var Timestamp = new Date(row.departure_time);
                                row.departure_time = Timestamp.toLocaleString();
                            }
                            if (!isNaN(row.arrival_time)) {
                                var Timestamp = new Date(row.arrival_time);
                                row.arrival_time = Timestamp.toLocaleString();
                            }
                            if (!isNaN(row.state)) {
                                var value = row.state;
                                var btn = "";
                                if (value == 1) {
                                    btn = "<button onclick='cancelo(" + row.oid + ")'  class=\"easyui-linkbutton\" style=\"width: 80px\">取消</button>" + "<button onclick='pay(" + row.oid + ")'  class=\"easyui-linkbutton\" style=\"width: 80px\">付款</button>";
                                }
                                if (value == 2) {
                                    btn = "<button onclick='cancelo(" + row.oid + ")'  class=\"easyui-linkbutton\" style=\"width: 80px\">取消</button>" + "<button onclick='print(" + row.oid + ")'  class=\"easyui-linkbutton\" style=\"width: 80px\">取票</button>";
                                }
                                if (value == 3) {
                                    btn = "已退款";
                                }
                                if (value == 4) {
                                    btn = "已取票";
                                }
                            }
                            return '<div><table><tr>' +
                                '<p>订单id: ' + row.oid + '</p>' +
                                '<p>车次号: ' + row.trainno + '</p>' +
                                '<p>出发时间: ' + row.departure_time
                                + '</p>' +
                                '<p>到达时间: ' + row.arrival_time + '</p>' +
                                '<p>始发站: ' + row.dstation + '</p>' +
                                '<p>终点站: ' + row.terminal + '</p>' +
                                '<p>票价: ' + row.price + '</p>' +
                                '<p>车厢号: ' + row.carriage + '</p>' +
                                '<p>座位号: ' + row.seatno + '</p>' +
                                '<p> ' + btn + '</p>' +
                                '</tr></table></div>';
                        }
                    });

                } else {
                    alert("error");
                }
            }
        }
    }

}

function print(oid) {
    var req = createXMLHTTPRequest();
    if (req) {
        req.open("POST", "Orders/printOrder", true);
        req.setRequestHeader("Content-Type",
            "application/x-www-form-urlencoded; charset=utf-8;");
        req.send("oid=" + oid);
        req.onreadystatechange = function () {
            if (req.readyState == 4) {
                if (req.status == 200) {
                    var msg = req.responseText;
                    if (msg == "fail1") {
                        alert("订单不存在");
                    }
                    else if (msg == "error1") {
                        alert("订单状态异常，请刷新界面");
                    }
                    else {
                        var order = JSON.parse(msg);
                        if (!isNaN(order.departure_time)) {
                            var Timestamp = new Date(order.departure_time);
                            order.departure_time = Timestamp.toLocaleString();
                        }
                        if (!isNaN(order.arrival_time)) {
                            var Timestamp = new Date(order.arrival_time);
                            order.arrival_time = Timestamp.toLocaleString();
                        }
                        alert(
                            '订单id: ' + order.oid + '\n车次号: ' + order.trainno +
                            '\n出发时间: ' + order.departure_time +
                            '\n到达时间: ' + order.arrival_time +
                            '\n始发站: ' + order.dstation +
                            '\n终点站: ' + order.terminal +
                            '\n票价: ' + order.price +
                            '\n车厢号: ' + order.carriage +
                            '\n座位号: ' + order.seatno + '\n')
                    }
                    $("#tb").datagrid({
                        title: "我的订单",
                        url: 'Orders/getAll',
                        fitColumns: true,
                        singleSelect: true,
                        pagination: true,//分页属性
                        columns: [[
                            {field: 'onumber', title: '订单编号', width: 100},
                            {field: 'trainno', title: '车次号', width: 100},
                            {
                                field: 'sell_time', title: '购票时间', width: 100,
                                formatter: function (value) {
                                    if (!isNaN(value)) {
                                        var Timestamp = new Date(value);
                                        return Timestamp.toLocaleString();
                                    }
                                }
                            },
                            {field: 'price', title: '票价', width: 100},
                            {
                                field: 'state', title: '订单状态', width: 100,
                                formatter: function (value) {
                                    if (!isNaN(value)) {
                                        if (value == 1) {
                                            return "未付款";
                                        }
                                        if (value == 2) {
                                            return "已付款";
                                        }
                                        if (value == 3) {
                                            return "已退款";
                                        }
                                        if (value == 4) {
                                            return "已取票";
                                        }
                                    } else {
                                        return value;
                                    }
                                }
                            }
                        ]],
                        view: detailview,
                        detailFormatter: function (index, row) {
                            if (!isNaN(row.departure_time)) {
                                var Timestamp = new Date(row.departure_time);
                                row.departure_time = Timestamp.toLocaleString();
                            }
                            if (!isNaN(row.arrival_time)) {
                                var Timestamp = new Date(row.arrival_time);
                                row.arrival_time = Timestamp.toLocaleString();
                            }
                            if (!isNaN(row.state)) {
                                var value = row.state;
                                var btn = "";
                                if (value == 1) {
                                    btn = "<button onclick='cancelo(" + row.oid + ")'  class=\"easyui-linkbutton\" style=\"width: 80px\">取消</button>" + "<button onclick='pay(" + row.oid + ")'  class=\"easyui-linkbutton\" style=\"width: 80px\">付款</button>";
                                }
                                if (value == 2) {
                                    btn = "<button onclick='cancelo(" + row.oid + ")'  class=\"easyui-linkbutton\" style=\"width: 80px\">取消</button>" + "<button onclick='print(" + row.oid + ")'  class=\"easyui-linkbutton\" style=\"width: 80px\">取票</button>";
                                }
                                if (value == 3) {
                                    btn = "已退款";
                                }
                                if (value == 4) {
                                    btn = "已取票";
                                }
                            }
                            return '<div><table><tr>' +
                                '<p>订单id: ' + row.oid + '</p>' +
                                '<p>车次号: ' + row.trainno + '</p>' +
                                '<p>出发时间: ' + row.departure_time
                                + '</p>' +
                                '<p>到达时间: ' + row.arrival_time + '</p>' +
                                '<p>始发站: ' + row.dstation + '</p>' +
                                '<p>终点站: ' + row.terminal + '</p>' +
                                '<p>票价: ' + row.price + '</p>' +
                                '<p>车厢号: ' + row.carriage + '</p>' +
                                '<p>座位号: ' + row.seatno + '</p>' +
                                '<p> ' + btn + '</p>' +
                                '</tr></table></div>';
                        }
                    });
                }
                else {


                }
            } else {
                // alert("error");
            }
        }
    }


}
